<?php
!defined('P_W') && exit('Forbidden');
if ($action == 'developer' || empty($action)) {
	if (empty($_POST['step'])) {
		InitGP(array('page'), '', 2);
		InitGP(array('name'));
		$page < 1 && $page = 1;
		$perpage = 20;
		$_sql_where = '';
		$name && $_sql_where .= ' AND name LIKE ' . pwEscape('%' . $name . '%');
		$limit = pwLimit(($page - 1) * $perpage, $perpage);
		$total = $db->get_value("SELECT COUNT(*) AS sum FROM pw_house_developer WHERE 1{$_sql_where}");
		$pages = numofpage($total, $page, ceil($total / $perpage), "$basename&action=developer&name=$name&");
		$devdb = array();
		$query = $db->query("SELECT * FROM pw_house_developer WHERE 1{$_sql_where} ORDER BY id $limit");
		while ($rt = $db->fetch_array($query)) {
			$devdb[] = $rt;
		}
	} else {
		$basename .= '&action=developer';
		InitGP(array('selid'), '', 2);
		empty($selid) && adminmsg('operate_error');
		if (is_array($selid)) {
			$houseinfo = House::LoadClass('HouseInfo');
			if ($info = $houseinfo->checkFieldData('developer', $selid, 'single')) {
				$devinfo = $db->get_one("SELECT * FROM pw_house_developer WHERE id = " . pwEscape($info['developer']));
				adminmsg($devinfo['name'] . '下存在楼盘，不能删除!');
			}
			$db->update("DELETE FROM pw_house_developer WHERE id IN (" . pwImplode($selid) . ')');
		}
		adminmsg('operate_success');
	}
} elseif ($action == 'adddeveloper') {
	$basename .= '&action=developer';
	InitGP(array('name', 'level', 'address', 'zipcode', 'email', 'url', 'principal', 'linkman', 'tel', 'fax', 
		'introduction'));
	empty($name) && adminmsg('开发商名称不能为空!');
	if ($db->get_one("SELECT * FROM pw_house_developer WHERE name=" . pwEscape($name))) {
		adminmsg('该开发商已经存在!');
	}
	$db->update("INSERT INTO pw_house_developer SET " . pwSqlSingle(array('name' => $name, 'level' => $level, 
		'address' => $address, 'zipcode' => $zipcode, 'email' => $email, 'url' => $url, 'principal' => $principal, 
		'linkman' => $linkman, 'tel' => $tel, 'fax' => $fax, 'introduction' => $introduction)));
	adminmsg('operate_success', $basename);
} elseif ($action == 'editdeveloper') {
	define('AJAX', 1);
	InitGP(array('id'), '', 2);
	$rt = $db->get_one("SELECT * FROM pw_house_developer WHERE id=" . pwEscape($id));
	empty($rt) && adminmsg('undefined_action');
	if (empty($_POST['step'])) {
		$posthash = EncodeUrl("$basename&action=editdeveloper", true);
		require_once PrintMode('ajax_info');
		ajax_footer();
	} else {
		InitGP(array('name', 'level', 'address', 'zipcode', 'email', 'url', 'principal', 'linkman', 'tel', 'fax', 
			'introduction'));
		empty($name) && adminmsg('开发商名称不能为空!');
		if ($name != $rt['name'] && $db->get_one("SELECT * FROM pw_house_developer WHERE name=" . pwEscape($name))) {
			adminmsg('该开发商已经存在!');
		}
		$db->update("UPDATE pw_house_developer SET " . pwSqlSingle(array('name' => $name, 'level' => $level, 
			'address' => $address, 'zipcode' => $zipcode, 'email' => $email, 'url' => $url, 'principal' => $principal, 
			'linkman' => $linkman, 'tel' => $tel, 'fax' => $fax, 'introduction' => $introduction)) . ' WHERE id=' . pwEscape($id));
		Showmsg("操作成功!\tjump\tadmin.php?adminjob=mode&admintype=house_developer&action=developer");
	}
} elseif ($action == 'deldeveloper') {
	define('AJAX', 1);
	InitGP(array('id'), '', 2);
	$posthash = EncodeUrl("$basename&action=developer", true);
	require_once PrintMode('ajax_info');
	ajax_footer();
}
include PrintMode('developer');
exit();
?>